package cn.jietuo.first.class03;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/6/21
 * @description:
 */
public class Code05_QuickSort {

    private static void process1(int[] arr, int l, int r) {
        if (l >= r) {
            return;
        }
        int m = partition1(arr, l, r);
        process1(arr, l, m - 1);
        process1(arr, m + 1, r);
    }

    public static int partition1(int[] arr, int l, int r) {
        if (l > r) {
            return -1;
        }
        if (l == r) {
            return l;
        }
        int lessIndex = l - 1;
        int currIndex = l;
        while (currIndex < r) {
            if (arr[currIndex] <= arr[r]) {
                swap(arr, currIndex, ++lessIndex);
            }
            currIndex++;
        }
        swap(arr, ++lessIndex, r);
        return lessIndex;
    }

    private static void swap(int[] arr, int i, int currIndex) {
        int temp = arr[i];
        arr[i] = arr[currIndex];
        arr[currIndex] = temp;
    }

    public static void quickSort1(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process1(arr, 0, arr.length - 1);
    }

    public static void quickSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process2(arr, 0, arr.length - 1);
    }

    public static void quickSort3(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process3(arr, 0, arr.length - 1);
    }

    private static void process3(int[] arr, int l, int r) {
        if (l >= r) {
            return;
        }
        swap(arr, l + (int)(Math.random() * (r - l + 1)), r);
        int[] m = partition3(arr, l, r);
        process1(arr, l, m[0] - 1);
        process1(arr, m[1] + 1, r);
    }

    private static int[] partition3(int[] arr, int l, int r) {
        if (l > r) {
            return new int[]{-1, -1};
        }
        if (l == r) {
            return new int[]{l, r};
        }
        int lessIndex = l - 1;
        int curIndex = l;
        int largeIndex = r + 1;
        int num = arr[r];
        while (curIndex < largeIndex) {
            if (arr[curIndex] > num) {
                swap(arr, curIndex, --largeIndex);
            } else if (arr[curIndex] == num) {
                curIndex++;
            } else {
                swap(arr, ++lessIndex, curIndex++);
            }
        }
        return new int[]{lessIndex + 1, largeIndex - 1};
    }

    private static void process2(int[] arr, int l, int r) {
        if (l >= r) {
            return;
        }
        int[] m = partition2(arr, l, r);
        process1(arr, l, m[0] - 1);
        process1(arr, m[1] + 1, r);
    }

    private static int[] partition2(int[] arr, int l, int r) {
        if (l > r) {
            return new int[]{-1, -1};
        }
        if (l == r) {
            return new int[]{l, r};
        }
        int lessIndex = l - 1;
        int curIndex = l;
        int largeIndex = r + 1;
        int num = arr[r];
        while (curIndex < largeIndex) {
            if (arr[curIndex] > num) {
                swap(arr, curIndex, --largeIndex);
            } else if (arr[curIndex] == num) {
                curIndex++;
            } else {
                swap(arr, ++lessIndex, curIndex++);
            }
        }
        return new int[]{lessIndex + 1, largeIndex - 1};
    }

    public static void main(String[] args) {
        int[] arr = new int[]{2, 4, 6, 3, 4, 1, 3};
        printArr(arr);
//        int i = partition1(arr, 0, arr.length - 1);
//        System.out.println("i = " + i);
        System.out.println("--------");
//        printArr(arr);
        quickSort3(arr);
        printArr(arr);
    }

    private static void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
